layui.define(function (exports) {
    var a = layui.jquery,
        f = layui.form,
        L = layui.laydate,
        T = {
            //select 联动
            linkageInit: function () {
                var name = '请选择';
                a('.sunSelect').each(function () {
                    var url = a(this).attr('data-url'), upid = 0, that = a(this);
                    that.find('[lay-filter=sunSelect]').each(function (i) {
                        getSelect(that, upid, i, '1');
                        upid = a(this).attr('data-id');
                    })
                })

                f.on('select(sunSelect)', function (data) {
                    var obj = a(data.elem),
                        parent = obj.parents('.sunSelect'),
                        parent_index = parent.index('.sunSelect'),
                        i = obj.index('.sunSelect:eq(' + parent_index + ') [lay-filter=sunSelect]');
                    parent.find('[lay-filter=sunSelect]:gt(' + i + ')').html('<option value="">' + name + '</option>');
                    getSelect(parent, data.value, i + 1);
                })

                //公共方法
                function getSelect(box, upid, index, is_create) {
                    var obj = box.find('[lay-filter=sunSelect]:eq(' + index + ')');
                    var data_id = is_create == '1' ? obj.attr('data-id') : '';
                    if (upid >= 0 && upid !== '') {
                        var url = box.attr('data-url');
                        a.get(
                            url,
                            {id: upid, level: obj.attr('data-level')},
                            function (data) {
                                createSelect(obj, data, box.attr('data-value'), box.attr('data-name'), data_id);
                            }, 'json'
                        )
                    } else {
                        obj.html('<option value="">' + obj.attr('data-name') + '</option>');
                        f.render();
                    }
                }

                function createSelect(obj, data, data_value, data_name, data_id) {
                    var options = '<option value="">' + obj.attr('data-name') + '</option>';
                    a.each(data, function (i) {
                        var selected = data_id == data[i][data_value] ? 'selected' : '';
                        options += '<option value="' + data[i][data_value] + '" ' + selected + '>' + data[i][data_name] + '</option>';
                    })
                    obj.html(options);
                    f.render();
                }
            },
            //初始化time组件
            timeInit: function () {
                a('.datetime,.lay-datetime').each(function () {
                    L.render({elem: this, type: 'datetime', trigger: 'click'})
                });

                a('.lay-time').each(function () {
                    L.render({elem: this, type: 'time', trigger: 'click'})
                });

                a('.date,.lay-date').each(function () {
                    L.render({elem: this, type: 'date', trigger: 'click'})
                });

                a('.lay-month').each(function () {
                    L.render({elem: this, type: 'month', trigger: 'click'})
                });

                a('.date_range,.lay-date-range').each(function () {
                    L.render({elem: this, type: 'date', range: true, trigger: 'click', rangeLinked: true})
                });

                a('.config_laydate').each(function () {
                    var format_1 = a(this).attr('data-format-1');
                    var format_2 = a(this).attr('data-format-2');
                    var range = format_2 == 1 ? false : true;
                    L.render({elem: this, type: format_1, range: range, trigger: 'click'})
                });
            },
        }


    exports('core', T);
});